package shajie.shajun.mmhf.View.Fragment;


import android.graphics.Color;
import android.view.View;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;
import shajie.shajun.mmhf.Base.BaseFragment;
import shajie.shajun.mmhf.Bean2.SearchOrder;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.ViewModel.Bmobdata;
import shajie.shajun.mmhf.databinding.FragmentPiechart30Binding;

/**
 * Created by Administrator on 2017/11/3.
 */

public class PieChart_30_Fragment extends BaseFragment<FragmentPiechart30Binding> {
    private FragmentPiechart30Binding binding;
    private int[] date;
    private PieChartData data;
    private PieChartView chart;
    private boolean hasCenterText1 = true;//环形中间的文字1
    private boolean hasCenterText2 = true;
    private boolean isExploded = true;
    private List<Integer> color=new ArrayList<Integer>();
    public static List<SearchOrder> orderList;
    @Override
    public int setlayoutid() {
        return R.layout.fragment_piechart_30;
    }
    @Override
    public void init(FragmentPiechart30Binding bingding) {
        binding=bingding;
        chart=bingding.Piechart;
       Bmobdata.get30order(getContext().getApplicationContext(),new Bmobdata.Singer<List<SearchOrder>>() {
            @Override
            public void success(List<SearchOrder> s) {
                orderList=s;
               orderpaiXu(s);
            }
            @Override
            public void faile(String s) {
                toast(s);
            }
        });
    }

    private void orderpaiXu(List<SearchOrder> orderList){
        ArrayList<String> names=new ArrayList<>();
        ArrayList<Integer> numbers=new ArrayList<>();
        for (SearchOrder order :
                orderList) {
            if (order.getProduct().equals("产品")){continue;}
            String[] arrayname;
            arrayname=order.getProduct().split("\\+");
            int num=order.getAmount();
            for (String name :
                    arrayname) {
                if (!names.contains(name)){
                    names.add(name);
                    numbers.add(num);
                }else {
                    int pos=names.indexOf(name);
                    numbers.set(pos,numbers.get(pos)+num);
                }
            }
        }
        int max=0;
        for (int i = 0; i <numbers.size() ; i++) {
            max+=numbers.get(i);
        }
        int n2=0;
        for (int i = 0; i <names.size()-1 ; i++) {
            if (numbers.get(i)<2){
                n2+=numbers.get(i);
                names.remove(i);
                numbers.remove(i);
                i--;
                continue;
            }
            for (int j = i+1; j <names.size() ; j++) {
                if (numbers.get(i)<numbers.get(j)){
                    int k=numbers.get(i);
                    numbers.set(i,numbers.get(j));
                    numbers.set(j,k);
                }
            }

        }
        names.add("其余");
        numbers.add(n2);
        DecimalFormat df = new DecimalFormat("#.0");
        ArrayList<String> paixu=new ArrayList<>();
        for (int i = 0; i <numbers.size()-1 ; i++) {
            paixu.add(df.format(((float)numbers.get(i)/(float)max)*100.0f));
        }
        paixu.add(df.format(((float)n2/(float)max)*100.0f));

        if (names.size()!=numbers.size()||names.size()!=paixu.size()){
            toast("数据出错!");
            return;
        }
        binding.orderpaihang.setVisibility(View.GONE);
        chart.setVisibility(View.VISIBLE);
        initViews();
        generateData(names,numbers,paixu);


    }

    private void color_load(){
        color.add(Color.RED);
        color.add(Color.BLUE);
        color.add(getResources().getColor(R.color.orange));
        color.add(getResources().getColor(R.color.skyblue));
    }

    private void initViews() {
        color_load();
//        chart.setOnValueTouchListener(new PieChartOnValueSelectListener() {
//            @Override
//            public void onValueSelected(int i, SliceValue sliceValue) {
//                Toast.makeText(getContext(),String.valueOf(i), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onValueDeselected() {
//
//            }
//        });//添加点击事件
        chart.setCircleFillRatio(0.9f);//设置图所占整个view的比例  当有外面的数据时使用，防止数据显示不全
        chart.setChartRotation(-90,true);////设置饼图旋转角度，且是否为动画
        chart.setChartRotationEnabled(false);//设置饼图是否可以手动旋转

    }

    /**
     * 生成数据
     */
    private void generateData(ArrayList<String> names,ArrayList<Integer> numbers,ArrayList<String> paixu) {
        //  int numValues = 10;//分成的块数

        List<SliceValue> values = new ArrayList<SliceValue>();
        for (int i = 0; i < names.size(); i++) {
            SliceValue sliceValue = new SliceValue(numbers.get(i),names.size()-1==i?Color.GRAY:color.get(i%4));//每一块的值和颜色，图标根据值自动进行比例分配
            sliceValue.setLabel(names.get(i)+"["+paixu.get(i)+"%]");
            values.add(sliceValue);
        }

        data = new PieChartData(values);
        data.setHasLabels(true);//显示数据
        data.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比
        data.setHasLabelsOutside(false);//占的百分比是否显示在饼图外面
        data.setHasCenterCircle(true);//是否是环形显示
        data.setCenterCircleScale(0.4f);////设置环形的大小级别
        data.setValueLabelBackgroundColor(Color.TRANSPARENT);////设置值得背景透明
        data.setValueLabelBackgroundEnabled(false);//数据背景不显示
        data.setValueLabelsTextColor(Color.WHITE); //数字颜色
        data.setValueLabelTextSize(13);

        //data.setValues(values);//填充数据
        if (isExploded) {
            data.setSlicesSpacing(1);//设置间隔为0
        }

        if (hasCenterText1) {
            data.setCenterText1("最近30天");//环形中间文字
            data.setCenterText1FontSize(20);
            data.setCenterText1Color(Color.WHITE);////设置值得颜色*/
        }
        if (hasCenterText2) {
            data.setCenterText2("售出产品");
           // Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Italic.ttf");
            //data.setCenterText2Typeface(tf);
            data.setCenterText2FontSize(15);
            data.setCenterText2Color(Color.YELLOW);////设置值得颜色*/
        }

        chart.setPieChartData(data);
    }


}
